
/**
	统一下单
*/

//@Bean
class UnifyOrderService {

	//@Autowired
	coinInvestService;

	//@Autowired
	okexOrderService;

	//@Autowired
	emailService;

	//@Autowired
	lifeCircleService;


	/**
		统一下单
	*/
	async doUnifyOrder(){

		const {coinList} =  this.coinInvestService;
		
		for(let i=0;i<coinList.length;i++){

			const coin = coinList[i];

			const {name,platform} = coin;

			let orderResult;

			switch(platform){

				case 'okex':orderResult = await this.okexOrderService.orderForCoin(coin);break;

				default: `not suport platform ${platform}`

			}

			//处理结果
			switch(orderResult.type){
				case 'buy':
					const {price,balance,realAmount} = orderResult;
					this.lifeCircleService.addAmount(realAmount,{[name]:price},balance);
					break;
				case 'sell':
					this.lifeCircleService.addSellAmount(orderResult.realAmount);
					break;
				case 'None':
					//手动更新最后订单时间
					this.lifeCircleService.updateLastOrderTime();
					break;
				default:
					throw `order result type error type=>[${type}]`
			}

			//发送邮件
			await this.emailService.orderSuccess(orderResult,this.lifeCircleService.data);

		}

	}



}


module.exports = {UnifyOrderService}